package com.example.demo.mapper;

import com.example.demo.entity.StudentCourseProgress;
import org.apache.ibatis.annotations.*;
import java.util.Optional;

@Mapper
public interface StudentCourseProgressMapper {

    // 列名与数据库表字段一致（无需修改，保持与表结构对齐）
    String COLUMNS = "id, student_id, course_id, completed_lessons, last_study_time, " +
            "progress_rate, assignment_id, completion_status, score, updated_at";

    // 插入进度记录：参数类型与 Entity 一致（studentId→String，courseId→int）
    @Insert("INSERT INTO student_course_progress (" +
            "student_id, course_id, completed_lessons, last_study_time, " +
            "progress_rate, assignment_id, completion_status, score, updated_at" +
            ") VALUES (" +
            "#{studentId}, #{courseId}, #{completedLessons}, #{lastStudyTime}, " +
            "#{progressRate}, #{assignmentId}, #{completionStatus}, #{score}, #{updatedAt}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 自增主键映射（id 为 Long，正常生效）
    int insert(StudentCourseProgress progress);

    // 根据学生ID和课程ID查询进度：参数类型同步改为 String（studentId）+ int（courseId）
    @Select("SELECT " + COLUMNS + " FROM student_course_progress " +
            "WHERE student_id = #{studentId} AND course_id = #{courseId}")
    Optional<StudentCourseProgress> findByStudentAndCourse(
            @Param("studentId") String studentId, // 对应 Entity 的 String 类型
            @Param("courseId") String courseId       // 对应 Entity 的 int 类型
    );

    // 更新进度记录：SQL 无需修改（占位符与 Entity 字段名一致，MyBatis 自动映射）
    @Update("UPDATE student_course_progress SET " +
            "completed_lessons = #{completedLessons}, " +
            "last_study_time = #{lastStudyTime}, " +
            "progress_rate = #{progressRate}, " +
            "assignment_id = #{assignmentId}, " +
            "completion_status = #{completionStatus}, " +
            "score = #{score}, " +
            "updated_at = #{updatedAt} " +
            "WHERE id = #{id}")
    int update(StudentCourseProgress progress);
}